import axios from '@/libs/axios.js'
import baseUrl from './index'
let jsonHeaders = {'content-type': 'application/json;charset=UTF-8'}



// GET /home
// 相关重点项目及科研
export const getScientificHome = () => {

  return axios({
    url: `${baseUrl}/scientific/home`,
    method: 'get',
    headers: jsonHeaders,
  })
}


// 首页科研分类统计
export const getScientStatic = () => {

  return axios({
    url: `${baseUrl}/scientific/home/statistics`,
    method: 'get',
    headers: jsonHeaders,
  })
}


// 增删改查的6类型table列表
export const getList = (params, data) => {
  let pageIndex = (params && params.pageIndex) || 1
  let pageSize = (params && params.pageSize) || 20

  let query={} // // 将没有数据的过滤掉，因为查询时间时候传空字符串报错
  for(let key in params){
    let value = params[key]
    if(value){
      query[key] = value
    }
  }

  return axios({
    url: `${baseUrl}/scientific/achievement`,//`${baseUrl}/ftp/filerecord/page`,
    method: 'get',
    params: query,
    headers: jsonHeaders,
  })
}

// 大屏幕统计
export const getHomeSummary = (params, data) => {
  return axios({
    url: `${baseUrl}/scientific/achievement/home`,//`${baseUrl}/ftp/filerecord/page`,
    method: 'get',
    headers: jsonHeaders,
  })
}
export const getAllProjects = (params, data) => {
  return axios({
    url: `${baseUrl}/scientific/projectManage`,//`${baseUrl}/ftp/filerecord/page`,
    method: 'get',
    headers: jsonHeaders,
  })
}





export const getAGroupyear = (params) => {
  let pageIndex = (params && params.pageIndex) || 1
  let pageSize = (params && params.pageSize) || 20

  let query={}
  for(let key in params){
    let value = params[key] // 将没有数据的过滤掉，因为查询时间时候传空字符串报错
    if(value){
      query[key] = value
    }
  }

  return axios({
    url: `${baseUrl}/scientific/achievement/group/year`,//`${baseUrl}/ftp/filerecord/page`,
    method: 'get',
    params: query,
    headers: jsonHeaders,
  })
}

export const getAInfo = (id) => {
  return axios({
    url: `${baseUrl}/scientific/achievement/${id}`,//`${baseUrl}/ftp/filerecord/page`,
    method: 'get',
    //data: data,
    headers: jsonHeaders,
  })
}

// 删除
export const delList = (ids) => {
  return axios({
    url: `${baseUrl}/scientific/achievement?ids=${ids}`,//`${baseUrl}/ftp/filerecord/${id}`,
    method: 'delete',
    headers: jsonHeaders,
  })
}


export const addAchievement = (data) => {
  return axios({
    url: `${baseUrl}/scientific/achievement`,
    method: 'post',
    data,
    headers: jsonHeaders
  })
}

export const editAchievement = (data) => {
  let id = data.id
  return axios({
    url: `${baseUrl}/scientific/achievement/${id}`,
    method: 'PUT',
    data,
    headers: jsonHeaders
  })
}

export const getDirectionForSelect = (type = '') => {
  return new Promise(resolve => {
    if(!type){
      let datanull = []
      resolve(datanull)
      return
    }

    let adict = sessionStorage.getItem('achievedict');  // 先取缓存的数据
    if(adict){
      let dict =  JSON.parse(adict)
      let data = dict[type]
      if(data && data.length>0){
        resolve(data)
        return
      }
    }




    axios({
      url: `${baseUrl}/dictionaries/dictTypes/selDictTypeAndDictEntitpInfo`,
      method: 'get',
      params: {
        dictTypeID: type
      },
      headers: jsonHeaders,
    }).then(res => {
      let { code, attribute } = res.data
      let dictlist = []
      if (code === 1) {
        if(res.data
          &&res.data.attribute
          &&res.data.attribute.attribute
          &&res.data.attribute.attribute[0]
          &&res.data.attribute.attribute[0].dictEntryList
        ){

          let list = res.data.attribute.attribute[0].dictEntryList||[]
          dictlist = list.map(item=>{
            item={
              //...item,
              label:item.dictname,
              value:item.dictid
            }
            return item
          })
        }
      }

      let dict = {}
      if(adict){
        dict =  JSON.parse(adict)
      }
      dict[type] = dictlist
      sessionStorage.setItem('achievedict',JSON.stringify(dict));


      resolve(dictlist)
    }).catch(res=>{
      console.log('获取字典数据错误dictTypeID=',type,res)
      let datanull = []
      resolve(datanull)

    })
  })
}

